(function($){

	var pageControl = {
		options: {
			totalItemCount: 0
			, displayItemCount: 10
			// , onPageChange: function(e, data) {}  // data: {currentPageNum:2}
		}
		
		, _create: function() {
			var self = this;
			var container = self.element;			
			var pageHtml = [
				'<div class="im-page-bar">',
				'	<a href="javascript:;" class="im-icon16 icon-page-first"></a>',
				'	<a href="javascript:;" class="im-icon16 icon-page-pre"></a>',
				'	<span class="page-input">',
				'		<input type="text" style="width:20px;" value="1" class="text-input">',
				'	</span>',
				'	<span class="page-count"></span>',
				'	<a href="javascript:;" class="im-icon16 icon-page-next"></a>',
				'	<a href="javascript:;" class="im-icon16 icon-page-last"></a>',
				'</div>',
			].join('');
			
			container.html(pageHtml);
			
			self._displayItemCount = self.options.displayItemCount;
			
			self.setTotalItemCount(self.options.totalItemCount)
			
			self._bindEvents();
		}
		
		, _events: {
			pageChange: 'onPageChange'
		}
		
		, _currentPageNum: 1
		
		, _totalPageNum: 0
		
		, _totalItemCount: 0
		
		, _displayItemCount: 0
		
		, _setOption: function(key, value) {
			$.Widget.prototype._setOption.apply(this, arguments);
		}
		
		, _bindEvents: function() {
			var self = this;
			var container = self.element;	
			
			var firstPageBtn = container.find('.icon-page-first');
			var lastPageBtn = container.find('.icon-page-last');	
			var prePageBtn = container.find('.icon-page-pre');	
			var nextPageBtn = container.find('.icon-page-next');	
			var pageTextInput = container.find('.text-input');
			var pageCountLabel = container.find('.page-count');
			
			firstPageBtn.bind('click', function() {
				self._currentPageNum = 1;
				pageTextInput.val(self._currentPageNum);
				self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
			});
			lastPageBtn.bind('click', function() {
				self._currentPageNum = self._totalPageNum;
				pageTextInput.val(self._currentPageNum);
				self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
			});
			prePageBtn.bind('click', function() {
				self._currentPageNum--;
				if (self._currentPageNum <= 0) {
					self._currentPageNum = 1;
				}
				pageTextInput.val(self._currentPageNum);
				self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
			});
			nextPageBtn.bind('click', function() {
				self._currentPageNum++;
				if (self._currentPageNum > self._totalPageNum) {
					self._currentPageNum = self._totalPageNum;
				}
				pageTextInput.val(self._currentPageNum);
				self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
			});
			pageTextInput.bind('keydown', function(e) {
                if (e.keyCode == 13) {
                    e.preventDefault();
                    self._currentPageNum = parseInt(pageTextInput.val());
                    if (self._currentPageNum <= 0) {
                    	self._currentPageNum = 1;
                    } else if (self._currentPageNum > self._totalPageNum) {
                    	self._currentPageNum = self._totalPageNum;
                    }
                    pageTextInput.val(self._currentPageNum);
                    self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
                }				
			});
		}
		
		, setTotalItemCount: function(totalCount) {
			var self = this;
			var container = self.element;	
			var pageCountLabel = container.find('.page-count');
			
			self._totalItemCount = totalCount;	
			self._totalPageNum = self._totalItemCount % self._displayItemCount == 0 ? self._totalItemCount / self._displayItemCount : parseInt(self._totalItemCount / self._displayItemCount) + 1;
			pageCountLabel.text('共' + self._totalPageNum + '页');					
		}
		
		, resetCurrentPageNum: function() {
			var self = this;
			var container = self.element;
			var pageTextInput = container.find('.text-input');
			
			self._currentPageNum = 1;
			pageTextInput.val(self._currentPageNum);
		}
		
		, getCurrentPageNum: function() {
			return this._currentPageNum;
		}
		
		, destroy: function() {
			$.Widget.prototype.destroy.call(this);
		}
	};
	
	$.widget('cf.pageControl', pageControl);
	
})(jQuery);